package com.paas.form.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.paas.common.cache.CacheKey;
import com.paas.common.util.BeanUtil;
import com.paas.common.web.SystemConst;
import com.paas.common.web.service.GenericCacheService;
import com.paas.form.dao.FormViewPubDao;
import com.paas.form.entity.FormAttributePubEntity;
import com.paas.form.entity.FormViewPubEntity;
import com.paas.form.service.IFormAttributePubService;
import com.paas.form.service.IFormViewPubService;
import com.paas.form.vo.ViewPublishVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 定版视图服务
 */
@Service("formViewPubService")
public class FormViewPubServiceImpl  extends GenericCacheService<FormViewPubDao,FormViewPubEntity> implements IFormViewPubService{

    @Autowired
    public IFormAttributePubService formAttributePubService;

    @Override
    public ViewPublishVo getViewFinalizeById(String viewId){
        // 根据id查询定版视图
        FormViewPubEntity formViewPubEntity = this.getById(viewId);
        if(BeanUtil.isEmpty(formViewPubEntity)){
            return null;
        }
        //定义返回对象
        ViewPublishVo viewPublishVo = new ViewPublishVo();
        BeanUtil.copyProperties(viewPublishVo,formViewPubEntity);
        //查询视图属性信息
        List<FormAttributePubEntity> formAttributeEntityList = formAttributePubService.getValidListByViewId(viewId);
        viewPublishVo.setAttributes(formAttributeEntityList);//填充主视图属性

        //查询子视图信息
        List<FormViewPubEntity> childrenViewList = this.getChildViewByViewId(viewId);
        if(BeanUtil.isNotEmpty(childrenViewList)){
            //子视图id集合
            List<String> childrenViewIds = childrenViewList.stream().map(FormViewPubEntity::getId).collect(Collectors.toList());
            Map<String,List<FormAttributePubEntity>> childrenAttributeMap = new HashMap<String,List<FormAttributePubEntity>>();
            //根据视图id集合查询属性
            List<FormAttributePubEntity> childrenAttributeList = formAttributePubService.getListByViewIds(childrenViewIds);
            if(BeanUtil.isNotEmpty(childrenAttributeList)){
                childrenAttributeMap = childrenAttributeList.stream().collect(Collectors.groupingBy(FormAttributePubEntity::getViewId));
            }
            //子视图定义对象
            List<ViewPublishVo> childrenList = new ArrayList<ViewPublishVo>();
            //设置子视图信息FormAttributePubEntity
            ViewPublishVo children = null;
            for(FormViewPubEntity temp :childrenViewList){
                children = new ViewPublishVo();
                BeanUtil.copyProperties(children,temp);
                if(childrenAttributeMap!=null && childrenAttributeMap.containsKey(temp.getId())){
                    children.setAttributes(childrenAttributeMap.get(temp.getId()));
                }
                childrenList.add(children);
            }
            viewPublishVo.setChildrenList(childrenList);
        }
        return viewPublishVo;
    }

    /**
     * 根据主视图查询子视图信息
     * @param viewId
     * @return
     */
    private List<FormViewPubEntity> getChildViewByViewId(String viewId){
        QueryWrapper<FormViewPubEntity> queryWrapper = new QueryWrapper<FormViewPubEntity>();
        queryWrapper.eq("view_id",viewId);
        return this.baseMapper.selectList(queryWrapper);
    }

//    @Override
//    public List<FormViewPubEntity> getChildViewByViewIdWithCache(String viewId){
//        String cacheKey = "children_"+viewId;
//        //从缓存中获取子视图
//        List<FormViewPubEntity> childrenList =( List<FormViewPubEntity>)this.getCacheByKey(cacheKey);
//        if(childrenList != null){
//            return childrenList;
//        }
//        QueryWrapper<FormViewPubEntity> queryWrapper = new QueryWrapper<FormViewPubEntity>();
//        queryWrapper.eq("view_id",viewId);
//        queryWrapper.eq("status", SystemConst.NORMAL_STATUS);
//        childrenList=  this.baseMapper.selectList(queryWrapper);
//        //数据添加至缓存
//        if(BeanUtil.isNotEmpty(childrenList) && childrenList.size()>0){
//            this.addCache(cacheKey,childrenList,2 * 60 * 60);
//        }else{
//            this.addCache(cacheKey,new ArrayList<>(),2 * 60 * 60);
//        }
//        return childrenList;
//    }

//    @Override
//    public FormViewPubEntity getByIdWithCache(String id){
//        FormViewPubEntity bean = (FormViewPubEntity) this.getCacheByKey(id);
//        if(bean!=null){
//            return bean;
//        }
//        bean = this.getById(id);
//        if(bean!=null){
//            this.addCache(id,bean,2 * 60 * 60);
//        }
//        return bean;
//    }

    @Override
    public void delByViewIds(List<String> viewIds){
        for(String viewId:viewIds){
            this.deleteCacheByKey(viewId);
            //有子视图缓存，清视图缓存
            String childCacheKey = "children_"+viewId;
            List<FormViewPubEntity> childrenList =( List<FormViewPubEntity>)this.getCacheByKey(childCacheKey);
            if(childrenList != null){
                this.deleteCacheByKey(childCacheKey);
            }
        }
    }


    @Override
    protected CacheKey buildCacheKey() {
        return new CacheKey("FORM_VIEWPUB_INFO","定版视图信息");
    }
}